package com.suneee.trades.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.trades.dao.ViewShoppDetailDao;
import com.suneee.trades.entity.ViewShoppDetail;

@Repository("viewShoppDetailDao")
public class ViewShoppDetailDaoImpl extends GenericDao<ViewShoppDetail, Integer> implements ViewShoppDetailDao{

	@Override
	public List<ViewShoppDetail> batchShoppDetailByShoppId(Integer shoppId,boolean flag,String tid) 
	{
		List<Object> list = null;
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == shopList || shopList.size() == 0)
		{
			list = new ArrayList<Object>();
			list.add(" ");
		}
		WhereClauseBuilder wcb = new WhereClauseBuilder();
		wcb.equals("shopp_id", shoppId);
		wcb.in("shop_id", list);
		if(flag)
		{
			wcb.appendWhere( " sku_id NOT IN (SELECT SUBSTRING_INDEX(ap.sku_str,'|',1) as sku_id FROM t_refund_apply ap WHERE ap.tid IN (SELECT DISTINCT tid FROM t_shopp_detail WHERE shopp_id = "+ shoppId +")  AND ap.process_status != 'DENY' AND ap.after_sale_type IN ('REFUND_GOODS','REFUND_MONEY')) " );	
		}
		//AND NOT EXISTS (SELECT 1 FROM t_refund_apply ap WHERE ap.tid = tid AND SUBSTRING_INDEX(ap.sku_str,'|',1) = sku_id AND ap.process_status != 'DENY')
		return this.selectWhere(wcb.getSql(), wcb.getArgs());
	}

}
